package spotGUI.ElementViewer;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import spotGUI.utils.EmptyContentAreaButton;
import elements.Element;
import javax.swing.border.EmptyBorder;

import managers.socialNetworkManagers.ElementsManager;

public class ElementPanel extends JPanel {
	
	private ElementViewer elementViewer;
	private AddElementPanel addElementPanel;
	private JScrollPane scrollPaneAddElement;
	private EmptyContentAreaButton btnFeatures;
	private JButton btnAggiorna;

	private Font font = new Font("Agency FB", Font.BOLD | Font.ITALIC, 18);
	
	public ElementPanel () {
		this (ElementsManager.ALL_USERS);
	}
	
	public ElementPanel(int userId) {
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{100, 0, 0};
		gridBagLayout.rowHeights = new int[]{203, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		btnAggiorna = new EmptyContentAreaButton(ElementPanel.class.getResource("/images/Icons/32x32/update.png"), ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("ElementPanel.2"));
		btnAggiorna.setMultiClickThreshhold(500L);
		btnAggiorna.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnAggiorna.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});
		btnAggiorna.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnAggiorna.setHorizontalTextPosition(SwingConstants.RIGHT);
		btnAggiorna.setFont(font);
		GridBagConstraints gbc_btnAggiorna = new GridBagConstraints();
		gbc_btnAggiorna.fill = GridBagConstraints.BOTH;
		gbc_btnAggiorna.insets = new Insets(0, 0, 5, 5);
		gbc_btnAggiorna.gridx = 0;
		gbc_btnAggiorna.gridy = 1;
		add(btnAggiorna, gbc_btnAggiorna);
		
		scrollPaneAddElement = new JScrollPane();
		scrollPaneAddElement.setVisible(false);
		scrollPaneAddElement.setMinimumSize(new Dimension(0, 272));
		GridBagConstraints gbc_scrollPaneAddElement = new GridBagConstraints();
		gbc_scrollPaneAddElement.gridwidth = 2;
		gbc_scrollPaneAddElement.fill = GridBagConstraints.BOTH;
		gbc_scrollPaneAddElement.gridx = 0;
		gbc_scrollPaneAddElement.gridy = 2;
		add(scrollPaneAddElement, gbc_scrollPaneAddElement);
		
		addElementPanel = new AddElementPanel(this);
		scrollPaneAddElement.setViewportView(addElementPanel);
		
		
		elementViewer = new ElementViewer(userId, this);
		GridBagConstraints gbc_elementViewer = new GridBagConstraints();
		gbc_elementViewer.gridwidth = 2;
		gbc_elementViewer.insets = new Insets(0, 0, 5, 0);
		gbc_elementViewer.fill = GridBagConstraints.BOTH;
		gbc_elementViewer.gridx = 0;
		gbc_elementViewer.gridy = 0;
		add(elementViewer, gbc_elementViewer);
		
		btnFeatures = new EmptyContentAreaButton(ElementPanel.class.getResource("/images/Icons/32x32px/edit.png"),
				ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("ElementPanel.1"));
		btnFeatures.setBorder(new EmptyBorder(0, 0, 0, 6));
		btnFeatures.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (scrollPaneAddElement.isVisible())
					scrollPaneAddElement.setVisible(false);
				else
					scrollPaneAddElement.setVisible(true);
				validate();
			}
		});
		btnFeatures.setFont(font);
		btnFeatures.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnFeatures.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnFeatures.setHorizontalTextPosition(SwingConstants.RIGHT);
		GridBagConstraints gbc_emptyContentAreaButton = new GridBagConstraints();
		gbc_emptyContentAreaButton.fill = GridBagConstraints.VERTICAL;
		gbc_emptyContentAreaButton.anchor = GridBagConstraints.EAST;
		gbc_emptyContentAreaButton.insets = new Insets(0, 0, 5, 0);
		gbc_emptyContentAreaButton.gridx = 1;
		gbc_emptyContentAreaButton.gridy = 1;
		add(btnFeatures, gbc_emptyContentAreaButton);
		
	
	}

	public void update() {
		elementViewer.clearAndUpdate();
	}

	public void setSelectedElement(Element element) {
		addElementPanel.setSelectedElement (element);
		
	}
	
}
